package com.vehicle.service.imp;

import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.RemindMapper;
import com.vehicle.pojo.*;
import com.vehicle.service.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class MyQuartzServiceImpl implements MyQuartzService {
    private Logger logger = Logger.getLogger(this.getClass());
    //7天的毫秒数
    private long time=7*24*60*60*1000;
    @Autowired
    private MaintainRecordService maintainRecordService;
    @Autowired
    private InsuranceRecordService insuranceRecordService;
    @Autowired
    private ASRecordService asRecordService;
    @Autowired
    private RemindMapper remindMapper;
    @Override
    public void execute() {
        logger.info("定时任务"+new Date());
        //增加保养提醒
        addMaintainRemind();
        //增加年检提醒
        addASRemind();
        //增加保险提醒
        addInsuranceRemind();
    }
    //增加保养提醒
    public void addMaintainRemind(){
        DataGridView maintainData=maintainRecordService.findMaintainRecord(new MaintainRecord());
        List<MaintainRecord> maintainRecords = (List<MaintainRecord>)maintainData.getData();
        //遍历保养部分
        for(MaintainRecord maintainRecord:maintainRecords){
            //如果下次保养时间-现在时间的毫秒数 小于7天的毫秒数
            if((maintainRecord.getNext_maintaintime().getTime()-new Date().getTime())<time){
                //先查询remind 循环查询是否有这辆车的保养提醒
                String carPlate=maintainRecord.getCars().getCarplate();
                List<Remind> reminds=remindMapper.selectAll(new Remind());
                //这里遍历查询的集合 循环判断  如果有 flag =false 如果没有 flag=true
                int count=0;
                for(Remind remind:reminds){
                    if(remind.getRemindplate().equals(carPlate)){
                        count++;
                    }
                }
                if(count==0){
                    Remind r = new Remind();
                    //设置车牌号
                    r.setRemindplate(carPlate);
                    r.setRemindtime(maintainRecord.getNext_maintaintime());
                    //提醒类型
                    Rtype rtype = new Rtype();
                    //保养提醒
                    rtype.setRtypeid(3);
                    r.setRtype(rtype);
                    //这里调用 增加提醒的 业务  预留接口
                    remindMapper.insertRemind(r);
                }
            }
        }
    }

    //增加年检提醒
    public void addASRemind(){
        //遍历年检
        DataGridView asRecordDate = asRecordService.findAllASRecord(new ASRecord());
        List<ASRecord>asRecords =(List<ASRecord>)asRecordDate.getData();
        for(ASRecord asRecord:asRecords){
            //如果下次年检时间-现在的时间的毫秒数小于7天的毫秒数
            if(asRecord.getNext_astime().getTime()-new Date().getTime()<time){
                //先查询remind 循环查询是否有这辆车的年检记录提醒
                String carPlate=asRecord.getCar().getCarplate();
                List<Remind> reminds = remindMapper.selectAll(new Remind());
                //这里遍历查询的集合 循环判断 如果有相同的  就不添加
                int count=0;
                for (Remind remind:reminds){
                    if(remind.getRemindplate().equals(carPlate)){
                        count++;
                    }
                }
                if(count==0){
                    Remind r = new Remind();
                    //设置车牌号
                    r.setRemindplate(carPlate);
                    r.setRemindtime(asRecord.getNext_astime());
                    //提醒类型
                    Rtype rtype = new Rtype();
                    //年检提醒
                    rtype.setRtypeid(2);
                    r.setRtype(rtype);
                    //这里调用 增加提醒的 业务  预留接口
                    remindMapper.insertRemind(r);
                }
            }
        }
    }

    //增加保险提醒
    public void addInsuranceRemind(){
        //遍历保险
        DataGridView insuranceData = insuranceRecordService.findAllInsuranceRecord(new InsuranceRecord());
        List<InsuranceRecord> insuranceRecords =(List<InsuranceRecord>) insuranceData.getData();
        for(InsuranceRecord insuranceRecord:insuranceRecords){
            //如果保险结束时间-现在的时间的毫秒数小于7天的毫秒数
            if(insuranceRecord.getInsurance_endtime().getTime()-new Date().getTime()<time){
                //先查询remind 循环查询是否有这辆车的年检记录提醒
                String carPlate=insuranceRecord.getCar().getCarplate();
                List<Remind> reminds=remindMapper.selectAll(new Remind());
                //这里遍历查询的集合 循环判断  如果有 flag =false 如果没有 flag=true
                 int count=0;
                for (Remind remind:reminds){
                    if(remind.getRemindplate().equals(carPlate)){
                       count++;
                    }
                }
                if(count==0){
                    Remind r = new Remind();
                    //设置车牌号
                    r.setRemindplate(carPlate);
                    r.setRemindtime(insuranceRecord.getInsurance_endtime());
                    //提醒类型
                    Rtype rtype = new Rtype();
                    //年检提醒
                    rtype.setRtypeid(1);
                    r.setRtype(rtype);
                    //这里调用 增加提醒的 业务  预留接口
                    remindMapper.insertRemind(r);
                }
            }
        }
    }

    @Override
    public void deleteOverdueReminder() {
        List<Remind>reminds=remindMapper.selectAll(new Remind());
        logger.info("开始检测是否有已过期提醒");
        logger.info("开始检测是否有过期7天的提醒");
        for(Remind remind:reminds){
            //如果现在时间的毫秒数 大于到期时间的毫秒数  说明已过期 更改状态
            if(remind.getRemindtime().getTime()<new Date().getTime()){
                remindMapper.updateStatus(remind.getRemindid());
            }
            //如果现在时间的毫秒数减去到期时间的毫秒数大于7天的毫秒数 说明以过期7天 执行删除
            if(new Date().getTime()-remind.getRemindtime().getTime()>time){
                remindMapper.deleteRemind(remind.getRemindid());
            }
        }
    }
}
